<script setup lang='ts'>
import { ref, watch } from 'vue';
import { addRootUser,addAnn,addBuy } from "../../../../api/api";
import { addMaterialData } from "../../../../api/Material";
import { addTitle } from "../../../../api/titleApi";
import { ElMessage } from 'element-plus'
const { centerDialogVisible,
  title,
  user,
  password,
  thisPage,
  FileData,
  MaterialData,
  myData,
  page,
  name,
  ann,
  size,
  money,
  integral,
  thisSelectData,
  types
} = defineProps < {
  centerDialogVisible: boolean
  ann?:string
  title: string
  user?: string
  money?:string
  integral?:string
  password?: string
  thisPage?: number
  FileData?: any
  MaterialData?: any
  myData?: any
  page?: any
  name?: any
  selectData?: string
  size?: any
  thisSelectData?:any
  types?:any
}> ()
const inFo = ref(centerDialogVisible)
//监视数据
watch(() => centerDialogVisible, (newVal) => {
  inFo.value = newVal
})
// 定义 emit 函数
const emit = defineEmits(['update:centerDialogVisible', 'update:newData']);

// 定义一个方法来改变 centerDialogVisible 并发出事件
const toggleDialog = () => {
  emit('update:centerDialogVisible', !centerDialogVisible);
};

//添加数据
const sendData = async () => {
  emit('update:centerDialogVisible', !centerDialogVisible);
  switch (title) {
    case '添加管理员':
      if (user && password && thisPage !== undefined) {
        const res = await addRootUser({ user, password, thisPage: thisPage })
        emit('update:newData', res.data);
      }
      break;
    case '素材大全':
      const formData = new FormData()
      formData.append('img', FileData)
      formData.append('data', MaterialData)
      formData.append('page', page)
      formData.append('name', name)
      formData.append('size', size)
      formData.append('selectData', thisSelectData)
      formData.append('types', types)
      const resaddMaterialData = await addMaterialData(formData)
      emit('update:newData', resaddMaterialData.data);
      break;
    case '添加分类':
      const formDatatitle = new FormData()
      formDatatitle.append('img', FileData)
      formDatatitle.append('myData', myData)
      formDatatitle.append('page', page)
      const res = await addTitle(formDatatitle)
      emit('update:newData', res.data);
      break;
      case '添加公告':
      const resAnn = await addAnn({ann:ann as string})
      if(resAnn.data.code !== '200') return ElMessage(resAnn.data.msg)
      emit('update:newData', resAnn.data.data);
      break;
      case '添加数据':
      const resData = await addBuy({money:money as string,integral:integral as string})
      if(resData.data.code !== '200') return ElMessage(resData.data.msg)
      emit('update:newData', resData.data.data);
      break;
  }

}
</script>

<template>
  <el-dialog v-model="inFo" :title :show-close="false" width="500" draggable align-center>
    <slot>1</slot>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="toggleDialog">取消</el-button>
        <el-button type="primary" @click="sendData">
          确定
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<style scoped></style>